package com.wxw.gulidemo.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wxw.gulidemo.R;
import com.wxw.gulidemo.entity.EduTeacher;
import com.wxw.gulidemo.entity.vo.TeacherQuery;
import com.wxw.gulidemo.service.IEduTeacherService;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 *      定义接口说明和参数说明————————
 *      定义在类上： @Api
 *      定义在方法上： @ApiOperation
 *      定义在参数上： @ApiParam
 *
 *
 * @author xinxinzi
 * @since 2022-05-26
 */
@RestController
@CrossOrigin(allowCredentials = "true")     //解决跨域问题
@RequestMapping(value = "/eduService/edu-teacher")
public class EduTeacherController {

    @Autowired
    private IEduTeacherService eduTeacherService;

    //查询讲师表所有数据
    @GetMapping("/findAll")
    @ApiOperation("查询讲师表所有数据")
    public R list(){
        return R.ok().data("items",eduTeacherService.list(null));
    }

    //逻辑删除讲师
    @DeleteMapping("/deleteTeacherById/{id}")
    @ApiOperation("逻辑删除讲师")
    public R deleteTeacherById(@PathVariable String id){
        boolean flag = eduTeacherService.removeById(id);
        if(flag == false){
            return R.error();
        }else {
            return R.ok();
        }
    }

    //分页查询      page：当前页 limit：每页显示记录数
    @GetMapping("pageFindAll/{page}/{limit}")
    public R pageFindAll(@PathVariable Long page, @PathVariable Long limit){
        //创建Page对象
        Page<EduTeacher> teacherPage = new Page<>(page,limit);
        //分页查询
        eduTeacherService.page(teacherPage,null);
        //获取分页后查询到的数据
        List<EduTeacher> records = teacherPage.getRecords();
        //获取总页数
        long total = teacherPage.getTotal();
        //返回结果
        return R.ok().data("total",total).data("records",records);
    }

    //分页带条件组合查询     @RequestBody将json数据封装到teacherQuery中
    @PostMapping("/pageTeacherCondition/{page}/{limit}")
    public R pageTeacherCondition(@PathVariable Long page,
                             @PathVariable Long limit,
                             @RequestBody(required = false) TeacherQuery teacherQuery){
        Page<EduTeacher> teacherPage = new Page<>(page,limit);
        eduTeacherService.pageQuery(teacherPage,teacherQuery); //调用pageQuery传入分页和实体类对象
        List<EduTeacher> rows = teacherPage.getRecords();
        long total = teacherPage.getTotal();
        return R.ok().data("total",total).data("rows",rows);
    }

    //新增讲师
    @ApiModelProperty(value = "新增讲师")
    @PostMapping("/save")
    public R save(@RequestBody EduTeacher eduTeacher){
        boolean flag = eduTeacherService.save(eduTeacher);
        if (flag){
            return R.ok();
        }else {
            return R.error();
        }
    }

    //根据id查询,用于信息回显
    @ApiModelProperty(value = "根据id查询")
    @GetMapping("/getById/{id}")
    public R getById(@PathVariable String id){
        EduTeacher teacher = eduTeacherService.getById(id);
        return R.ok().data("item",teacher);
    }

    //修改讲师
    @ApiModelProperty(value = "修改讲师")
    @PostMapping("/updateById")
    public R updateById(@RequestBody EduTeacher teacher){
        boolean flag = eduTeacherService.updateById(teacher);
        if (flag){
            return R.ok();
        }else {
            return R.error();
        }
    }
}
